﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Domain.Interface;
using Domain.Model;
using Domain.Model.Domain_Personal;
using Newtonsoft.Json;
using WebAPI.Attributes;

namespace WebAPI.Controllers
{
    public class PingShenController : ApiController
    {
        public IPingShenCaoZuo _ipingshen { set; get; }
        public IJiaoZhiGong _IJiaoZhiGong { set; get; }

        [HttpGet, WriteFileExceptionAspect]
        public List<Domain_MyKaoHeBiao> GetKaoHeFanWeiKHB(string datetimes, int mbid, string username)
        {
            var str = _ipingshen.GongZuoJiHuaYuanGong(datetimes, mbid, username);
            var lst = new List<Domain_MyKaoHeBiao>();
            lst = JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiao>>(str);
            return lst;
        }
        [HttpGet, WriteFileExceptionAspect]
        public List<Domain_MyKaoHeBiao> GetAllIngKaoHeBiaos(int mbid, string username)
        {
            var str = _ipingshen.GetAllIngKaoHeBiaos(mbid, username);
            var lst = new List<Domain_MyKaoHeBiao>();
            lst = JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiao>>(str);
            return lst;
        }
        [HttpGet, WriteFileExceptionAspect]
        public List<Domain_MyKaoHeBiao> GetKaoHeBiaosByKeShenHe(string datetimes, int mbid, string username)
        {
            var str = _ipingshen.GetKaoHeBiaosByKeShenHe(datetimes, mbid, username);
            var lst = new List<Domain_MyKaoHeBiao>();
            lst = JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiao>>(str);
            return lst;
        }
        [HttpGet]
        public Domain_MyKaoHeBiao GetKaoHeBiaoInfo(int mkhbid, string username)
        {
            var str = _ipingshen.GetKaoHeBiaoInfo(mkhbid, username);
            var lst = new Domain_MyKaoHeBiao();
            lst = JsonConvert.DeserializeObject<Domain_MyKaoHeBiao>(str);
            return lst;
        }

        [HttpGet]
        public List<Domain_PersonalMain> GetKaoHeBiaos(string username, string khusername)
        {
            var str = _ipingshen.GetKaoHeBiaos(username, khusername);
            var lst = new List<Domain_PersonalMain>();
            lst = JsonConvert.DeserializeObject<List<Domain_PersonalMain>>(str);
            foreach (var item in lst)
            {
                foreach (var item2 in item.PersonalPath)
                {
                    foreach (var item3 in item2.PersonalContents)
                    {
                        var usrstr = _IJiaoZhiGong.GetJiaoZhiGongInfo(item3.gzcdrid.ToString());
                        var info = JsonConvert.DeserializeObject<Domain_JiaoZhiGong>(usrstr);
                        item3.gzcdr = info.XingMing;
                    }
                }
            }
            
            return lst;
        }

        public List<dynamic> GetBeiKaoHeBiaos(string username)
        {
            var str = _ipingshen.GetBeiKaoHeBiaos(username);
            var lst = new List<dynamic>();
            lst = JsonConvert.DeserializeObject<List<dynamic>>(str);
            return lst;
        }
        [HttpGet]
        public Domain_MyKaoHeBiao GetKaoHeBiaoInfoGongZuoJiHua(int mkhbid, string username)
        {
            var str = _ipingshen.GetKaoHeBiaoInfoGongZuoJiHua(mkhbid, username);
            var lst = new Domain_MyKaoHeBiao();
            lst = JsonConvert.DeserializeObject<Domain_MyKaoHeBiao>(str);
            return lst;
        }
        [HttpGet]
        public void CreateWanZhongKaoHeBiao(int ksid, string username, string datetimes)
        {
            _ipingshen.CreateKaoHeBiaoJiXiao(ksid, username, datetimes);
        }

        [HttpGet]
        public void ReviewKaoHeBiao(int ksid, string username)
        {
            _ipingshen.DelKaoHeBiaoKeShi(ksid, username);
        }
        [HttpGet]
        public List<Domain_BeiKaoHeJiaoZhiGong> GetKaoHeRenYuanList(string username)
        {
            string str = _ipingshen.GetBieKaoHeRenYuans(username);
            var lst = new List<Domain_BeiKaoHeJiaoZhiGong>();
            lst = JsonConvert.DeserializeObject<List<Domain_BeiKaoHeJiaoZhiGong>>(str);
            return lst;
        }
        [HttpPost, WriteFileExceptionAspect]
        public string SavePingShengJieGuo(dynamic obj)
        {
            //var result = JsonConvert.DeserializeObject<dynamic>(str);

            var str = JsonConvert.SerializeObject(obj);
            Domain_KaoHeJieGuo model = JsonConvert.DeserializeObject<Domain_KaoHeJieGuo>(str);
            foreach (var item in model.data)
            {
                _ipingshen.UpdateDianPing(model.khbid,item.pcid,item.fz,model.username);
            }
            return str;
        }

        [HttpPost, WriteFileExceptionAspect]
        public void UpdateKaoHeBiao_TuiHui(dynamic obj)
        {
            _ipingshen.UpdateKaoHeBiao_TuiHui(int.Parse(obj.mkhbid.ToString()), obj.username.ToString(), obj.yijian.ToString());
        }

        /// <summary>
        /// 汇总表表头
        /// </summary>
        /// <param name="mbid"></param>
        /// <returns></returns>
        [HttpGet, WriteFileExceptionAspect]

        public Domain_HuiZongMoBan GetKaoHeBiaoMoBan(int mbid)
        {
            string str = _ipingshen.GetKaoHeHuiZongMoBan(mbid);
            var data = new Domain_HuiZongMoBan();
            data = JsonConvert.DeserializeObject<Domain_HuiZongMoBan>(str);
            return data;
        }
        /// <summary>
        /// 获取得分汇总表
        /// </summary>
        /// <param name="mbid"></param>
        /// <returns></returns>
        [HttpGet, WriteFileExceptionAspect]
        public List<Domain_HuiZongDeFen> GetKaoHeDeFen(int mbid, string sssj)
        {
            string str = _ipingshen.GetKaoHeHuiZongDeFen(mbid, sssj);
            var lst = new List<Domain_HuiZongDeFen>();
            lst = JsonConvert.DeserializeObject<List<Domain_HuiZongDeFen>>(str);
            return lst;
        }
        /// <summary>
        /// 删除评分
        /// </summary>
        /// <param name="mkhbid"></param>
        /// <param name="twoid"></param>
        /// <param name="username"></param>
        [HttpGet, WriteFileExceptionAspect]
        public void DelPingFen(int mkhbid, string username)
        {
            _ipingshen.DelPingFen(mkhbid, username);
        }

        public List<Domain_MyKaoHeBiaoPingFen> GetPingFenRenYuanList(int mkhbid)
        {
            string str = _ipingshen.GetPingFenRenYuanList(mkhbid);
            var lst = new List<Domain_MyKaoHeBiaoPingFen>();
            lst = JsonConvert.DeserializeObject<List<Domain_MyKaoHeBiaoPingFen>>(str);
            return lst;
        }
    }
}

